/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2021. All rights reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.kongnannan.example;

import cn.kongnannan.circularavatar.CircularImageView;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.element.PixelMapElement;
import ohos.media.image.PixelMap;

import java.util.ArrayList;

/**
 * MainAbility
 */
public class MainAbility extends Ability {
    private ArrayList<PixelMap> mBmps1 = new ArrayList<PixelMap>();
    private ArrayList<PixelMap> mBmps2 = new ArrayList<PixelMap>();
    private ArrayList<PixelMap> mBmps3 = new ArrayList<PixelMap>();
    private ArrayList<PixelMap> mBmps4 = new ArrayList<PixelMap>();
    private ArrayList<PixelMap> mBmps5 = new ArrayList<PixelMap>();
    private CircularImageView mImageView1;
    private CircularImageView mImageView2;
    private CircularImageView mImageView3;
    private CircularImageView mImageView4;
    private CircularImageView mImageView5;
    private Button onBitmapBtnClick;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        initValues();
    }

    private void initValues() {
        onBitmapBtnClick = (Button) findComponentById(ResourceTable.Id_onBitmapBtnClick);

        PixelMapElement avatar1 = ResUtil.getPixelMapDrawable(getContext(), ResourceTable.Media_headshow1);
        PixelMapElement avatar2 = ResUtil.getPixelMapDrawable(getContext(), ResourceTable.Media_headshow2);
        PixelMapElement avatar3 = ResUtil.getPixelMapDrawable(getContext(), ResourceTable.Media_headshow3);
        PixelMapElement avatar4 = ResUtil.getPixelMapDrawable(getContext(), ResourceTable.Media_headshow4);
        PixelMapElement avatar5 = ResUtil.getPixelMapDrawable(getContext(), ResourceTable.Media_headshow5);

        mBmps1.add(avatar1.getPixelMap());
        mBmps2.add(avatar1.getPixelMap());
        mBmps3.add(avatar1.getPixelMap());
        mBmps4.add(avatar1.getPixelMap());
        mBmps5.add(avatar1.getPixelMap());

        mBmps2.add(avatar2.getPixelMap());
        mBmps3.add(avatar2.getPixelMap());
        mBmps4.add(avatar2.getPixelMap());
        mBmps5.add(avatar2.getPixelMap());

        mBmps3.add(avatar3.getPixelMap());
        mBmps4.add(avatar3.getPixelMap());
        mBmps5.add(avatar3.getPixelMap());

        mBmps4.add(avatar4.getPixelMap());
        mBmps5.add(avatar4.getPixelMap());

        mBmps5.add(avatar5.getPixelMap());

        mImageView1 = (CircularImageView) findComponentById(ResourceTable.Id_circularImageView1);
        mImageView2 = (CircularImageView) findComponentById(ResourceTable.Id_circularImageView2);
        mImageView3 = (CircularImageView) findComponentById(ResourceTable.Id_circularImageView3);
        mImageView4 = (CircularImageView) findComponentById(ResourceTable.Id_circularImageView4);
        mImageView5 = (CircularImageView) findComponentById(ResourceTable.Id_circularImageView5);

        mImageView1.setImageBitmaps(mBmps1);
        mImageView2.setImageBitmaps(mBmps2);
        mImageView3.setImageBitmaps(mBmps3);
        mImageView4.setImageBitmaps(mBmps4);
        mImageView5.setImageBitmaps(mBmps5);
        setValues();
    }

    private void setValues() {
        CircularImageView mImageView21 = (CircularImageView) findComponentById(ResourceTable.Id_circularImageView21);
        CircularImageView mImageView22 = (CircularImageView) findComponentById(ResourceTable.Id_circularImageView22);
        CircularImageView mImageView23 = (CircularImageView) findComponentById(ResourceTable.Id_circularImageView23);
        CircularImageView mImageView24 = (CircularImageView) findComponentById(ResourceTable.Id_circularImageView24);
        CircularImageView mImageView25 = (CircularImageView) findComponentById(ResourceTable.Id_circularImageView25);

        mImageView21.setImageBitmaps(mBmps1);
        mImageView22.setImageBitmaps(mBmps2);
        mImageView23.setImageBitmaps(mBmps3);
        mImageView24.setImageBitmaps(mBmps4);
        mImageView25.setImageBitmaps(mBmps5);

        CircularImageView mImageView31 = (CircularImageView) findComponentById(ResourceTable.Id_circularImageView31);
        CircularImageView mImageView32 = (CircularImageView) findComponentById(ResourceTable.Id_circularImageView32);
        CircularImageView mImageView33 = (CircularImageView) findComponentById(ResourceTable.Id_circularImageView33);
        CircularImageView mImageView34 = (CircularImageView) findComponentById(ResourceTable.Id_circularImageView34);
        CircularImageView mImageView35 = (CircularImageView) findComponentById(ResourceTable.Id_circularImageView35);

        mImageView31.setImageBitmaps(mBmps1);
        mImageView32.setImageBitmaps(mBmps2);
        mImageView33.setImageBitmaps(mBmps3);
        mImageView34.setImageBitmaps(mBmps4);
        mImageView35.setImageBitmaps(mBmps5);
        onBitmapBtnClick.setClickedListener(component -> {
            Intent secondIntent = new Intent();
            Operation operation = new Intent.OperationBuilder()
                    .withBundleName(getBundleName())
                    .withAbilityName(PixelMapAblity.class.getName())
                    .build();
            secondIntent.setOperation(operation);
            startAbility(secondIntent);
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
